package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishFlavourMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassNameDishServiceImpl
 * @Description TODO
 * @Author钰玟
 * @Date2025/2/13 下午4:24
 * @Version 1.0
 **/
@Service
public class DishServiceImpl  implements DishService {

    @Autowired
    DishMapper dishMapper;

    @Autowired
    DishFlavourMapper dishFlavourMapper;
    @Autowired
    SetMealDishMapper setMealDishMapper;
    @Autowired
    CategoryMapper categoryMapper;
    /**
     * 添加菜品
     * @param dishDTO 菜品信息
     */
    @Override
    @Transactional//开启事务
    public void addDishWithFlavour(DishDTO dishDTO) {
        Dish dish = new Dish();
        //插入一个菜品
        //无口味
        BeanUtils.copyProperties(dishDTO, dish);

        dishMapper.insert(dish);
        //获取dishID==>>主键回显
        Long id = dish.getId();

        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors != null && !flavors.isEmpty()) {
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(id);
            }
            //插入多个口味 flavour
            dishFlavourMapper.insertBatch(flavors);
        }

    }

    @Override
    @Transactional
    public PageResult pageQueryDish(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishVO> page = dishMapper.pageQueryDish(dishPageQueryDTO);

        Long total = page.getTotal();
        List records = page.getResult();
        return new PageResult(total, records);

    }
    /**
     * 批量删除菜品
     * @param ids 菜品id列表
     * @return 删除菜品结果
     */
    @Override
    public void deleteDish(List<Long> ids) {
        //判断当前菜品是否能够删除  菜品起售不能删除
        for (Long id : ids) {
            Dish dish = dishMapper.selectById(id);
            if(dish.getStatus() == StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        //是否关联了套餐
        List<Long> setMealIds = setMealDishMapper.selectSetmealByDishID(ids);
        if(setMealIds != null && !setMealIds.isEmpty()){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        //删除菜品
//        for (Long id : ids) {
//            dishMapper.deleteById(id);
//            dishFlavourMapper.deleteByDishId(id);
//        }
        //批量删除菜品
        dishMapper.deleteBatchById(ids);

        //批量删除菜品口味
        dishFlavourMapper.deleteBatchByDishId(ids);


    }

    /**
     * 根据id查询菜品
     * @param id 菜品id
     * @return 菜品信息
     */
    @Override
    public DishVO getDishWithFlavourById(Long id) {
        DishVO dishVO = new DishVO();
        //获取dish对象
        Dish dish = dishMapper.selectById(id);
        BeanUtils.copyProperties(dish, dishVO);
        //获取口味
        List<DishFlavor> flavoursByDishId = dishFlavourMapper.getFlavoursByDishId(id);
        if(flavoursByDishId != null && !flavoursByDishId.isEmpty()){
            dishVO.setFlavors(flavoursByDishId);
        }
        //获取菜品分类名称
        Category category = categoryMapper.selectCategoryById(dish.getCategoryId());
        if(category != null){
            dishVO.setCategoryName(category.getName());
        }
        return dishVO;
    }
    /**
     * 修改菜品
     * @param dishDTO 菜品信息
     * @return 修改菜品结果
     */
    @Override
    public void updateWithFlavour(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        //更新菜品信息

        Long id = dish.getId();
        dishMapper.update(dish);

        //更新口味信息
        //先删后改
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors != null && !flavors.isEmpty()){
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(id);
            }
            //删除原有口味
            dishFlavourMapper.deleteByDishId(id);
            //插入新的口味
            dishFlavourMapper.insertBatch(flavors);
        }

    }

    @Override
    public void updateDishStatus(Long id, Integer status) {
        Dish dish = dishMapper.selectById(id);
        dish.setStatus(status);
        dishMapper.update(dish);
        if(status == StatusConstant.DISABLE){
            List<Long> dishIds = new ArrayList<>();
            dishIds.add(id);
            List<Long> setMealIds = setMealDishMapper.selectSetmealByDishID(dishIds);

            if(setMealIds != null && !setMealIds.isEmpty()){
                for (Long setMealId : setMealIds) {
                    Setmeal SetMeal = Setmeal.builder().status(StatusConstant.DISABLE).id(setMealId).build();
                    setMealDishMapper.update(SetMeal);
                }
            }

        }

    }

    @Override
    public List<DishVO> getDishList(Long categoryId) {
        List<DishVO> dishVOS = dishMapper.selectDishListByCategoryId(categoryId);
        return dishVOS;
    }

    @Override
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavourMapper.getFlavoursByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }
}
